using FluentNHibernate.Mapping;
using NHibernate.Tool.hbm2ddl;
using NHibernateInsight.Tests.Domain;
using NHibernateInsight.Tests.Infrastructure;
using NUnit.Framework;

namespace NHibernateInsight.Tests.Insights.Basic
{
    [TestFixture]
    public class Exporting_db_schema : TestContext
    {
        private class Mappings_of
        {
            public class Product : ClassMap<Domain.Product>
            {
                public Product()
                {
                    Id(x => x.Id)
                        .GeneratedBy.Native();
                    HasManyToMany(x => x.Prospects)
                        .ParentKeyColumn("ProductId_Novi")
                        .ChildKeyColumn("ProspectId")
                        .Table("ProspectsProducts")
                        .Cascade.SaveUpdate()
                        .Inverse()
                        .AsBag();
                }
            }
            public class Prospect : ClassMap<Domain.Prospect>
            {
                public Prospect()
                {
                    Id(x => x.Id)
                        .GeneratedBy.Native();
                    Map(x => x.Sex)
                        .CustomSqlType("nvarchar(100)")
                        .Nullable()
                        .Index("IX_Prospects")
                        .Unique();
                    Map(x => x.Initials)
                        .CustomSqlType("nvarchar(100)")
                        .Not.Nullable()
                        .Index("IX_Prospects")
                        .UniqueKey("abc");
                    Map(x => x.Prefix)
                        .CustomSqlType("nvarchar(100)")
                        .Not.Nullable()
                        .Index("IX_Prefix")
                        .UniqueKey("abc");
                    HasOne(x => x.User)
                        .ForeignKey("UserId")
                        .Cascade.SaveUpdate();
                    HasManyToMany(x => x.Products)
                        .ParentKeyColumn("ProspectId")
                        .ChildKeyColumn("ProductId_Novi")
                        .Table("ProspectsProducts")
                        .Cascade.SaveUpdate()
                        .AsBag();
                }
            }
            public class User : ClassMap<Domain.User>
            {
                public User()
                {
                    Id(x => x.Id)
                        .GeneratedBy.Native();
                    Map(x => x.Prefix)
                        .CustomSqlType("nvarchar(100)")
                        .Nullable()
                        .Index("IX_Prefix");
                    HasMany(x => x.Prospects)
                        .KeyColumn("UserId")
                        .Inverse();
                }
            }            
        }
        
        [SetUp]
        public void Given_that()
        {
            PrepareForSql2005ExpressDb();

            DontCreateSchemaOnInit();
            HideCreateAndDropDbSqlFromConsole();

            Init(m => m.FluentMappings
                          .Add<Mappings_of.Product>()
                          .Add<Mappings_of.Prospect>()
                          .Add<Mappings_of.User>());
        }

        [TearDown]
        public void Cleanup_database()
        {
            Cleanup();
        }

        [Test]
        public void Export_of_Entities_creation_sql_can_be_done_to_console()
        {
            new SchemaExport(CurrentConfiguration).Execute(true, false, false, CurrentSession.Connection, null);
        }
    }
}